﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using AutoMapper;
using BLL;
using BLL.Interface;
using DAL;
using DAL.Models;
using ExpressManagement.Areas.BackSys.ServiceTools;
using ExpressManagement.Areas.BackSys.ServiceTools.Redis;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Caching.Memory;
using Newtonsoft.Json;

namespace ExpressManagement.Areas.BackSys.Controllers
{
    [Area("BackSys")]
    //[Route("api/[controller]")]
    //[ApiController]
    public class MessageController : BaseController
    {
        // GET: BackSys/Message
        public readonly IMessageImpl _impl;
        public readonly IMemoryCache _memoryCache;
   
        public MessageController(IMemoryCache _memoryCache, IMessageImpl _impl, RedisHelper redisHelper, ExpressDBContext _dbContext, IHttpContextAccessor httpContextAccessor, DbContextOptions<ExpressDBContext> _dbContextoptions, IMapper mapper, SignalRSender signalRSender)
            : base(mapper: mapper, signalRSender: signalRSender, httpContextAccessor: httpContextAccessor, _memoryCache: _memoryCache, redisHelper: redisHelper, _dbContext: _dbContext, dbContextOptions: _dbContextoptions)

        {
            //if (_impl == null) _impl = new MessageImpl();
            this._impl = _impl;
        }
        public async Task<IActionResult> MessageManage()
        {
            return PartialView();
        }

        [HttpPost]
        public async Task<IActionResult> Delete(int id = 0)
        {

            if (_impl.Delete(id) == true)
            {
                RemoveRedisCacheEntity(EntityMessage, TypePage, LoginType);
                return Json(1);
            }
            return Json(0);
        }


        [HttpGet]
        //[ResponseCache(Duration = 10)]
        public async Task<IActionResult> ChangePage([FromQuery]int page)
        {
            IDictionary<string, string> set = new Dictionary<string, string>();
            int? rows = GetInt32FromSession("messageRows");
            string keyStr = string.Empty;
            string cache = string.Empty;
            string serializeStr = string.Empty;
            int totalPages = 0;
          
            IList<Message> data = new List<Message>();
            cache = GetCacheFromRedis<string>(EntityMessage, TypePage, page, LoginType);
            if (!string.IsNullOrEmpty(cache) )
            {
                return Json(cache);
            }
            else
            {
                switch (LoginType)
                {
                    case "用户":
           
                            (totalPages, data) = _impl.GetList(rows.Value, page);
                            FillSetAndSaveCache(totalPages, JsonConvert.SerializeObject(data), page,set, EntityMessage, TypePage, LoginType,out serializeStr);
                            //set.Add("totalPages", totalPages.ToString());
                            //set.Add("data", JsonConvert.SerializeObject(data));
                            //serializeStr = JsonConvert.SerializeObject(set);
                            //SetCacheOnRedis<string>(EntityMessage, TypePage, page.Value, LoginType, serializeStr);
                        return Json(serializeStr);
                    case "管理员":
                    case "快递员":
                    
                            (totalPages, data) = _impl.GetList(rows.Value, page);
                        FillSetAndSaveCache(totalPages, JsonConvert.SerializeObject(data), page, set, EntityMessage, TypePage, LoginType, out serializeStr);
                        //set.Add("totalPages", totalPages.ToString());
                        //    set.Add("data", JsonConvert.SerializeObject(data));
                        //    serializeStr = JsonConvert.SerializeObject(set);
                        //    SetCacheOnRedis<string>(EntityMessage, TypePage, page.Value, LoginType, serializeStr);
                        return Json(serializeStr);

                    default:
                        return Json(JsonConvert.SerializeObject(set));
                }
                
            }

        
        }

    }
}